package com.example.xd.Service;

import com.example.xd.DAO.userMapper;
import com.example.xd.Entity.Video;
import jakarta.annotation.Resource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class VideoUploadService {

    @Autowired
    public userMapper userMapper;

    private static  String video_PATH = "D:" + File.separator + "fgobb" + File.separator + "upload_video";
    public static  String videoImage_PATH = "D:" + File.separator + "fgobb" + File.separator + "upload_videoThumbnail";

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor executor;

    // 异步上传视频文件的方法，将字节数组形式的视频数据保存为指定路径下的文件
    @Async("threadPoolTaskExecutor")
    public void uploadVideo(byte[] videoData, String fileName) {
        try {

                    File file = new File(video_PATH + File.separator + fileName);
            // 如果文件已存在，先删除原文件
            if (file.exists()) {
                file.delete();
            }
            Files.write(Paths.get(video_PATH + File.separator + fileName), videoData);
            // 同理
            // FileOutputStream fos = new FileOutputStream(video_PATH + File.separator + fileName);
            // fos.write(videoData);
            // fos.close();
        } catch (IOException e) {
            // 捕获文件写入过程中出现的异常
            throw new RuntimeException("视频文件上传失败: " + e.getMessage(), e);
        }
    }

    // 上传视频对应的图片文件
    public void uploadVideoImage(MultipartFile videoImage, String fileName) {

        try {
            File dir = new File(videoImage_PATH);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(videoImage_PATH + File.separator + fileName);
            // 如果文件已存在，删除原文件
            if (file.exists()) {
                file.delete();
            }
            Files.copy(videoImage.getInputStream(), Paths.get(videoImage_PATH + File.separator + fileName), StandardCopyOption.REPLACE_EXISTING);
            // 可以使用原代码的方式，使用Java NIO的方式处理文件复制更简洁
            // videoImage.transferTo(file);

        } catch (IOException e) {
            // 捕获并处理文件复制过程中出现的异常
            throw new RuntimeException("视频图片上传失败: " + e.getMessage(), e);
        }
    }

    //获取是否有同名的视频
    public boolean getVideoNameCount(String videoName) {
        int count =  userMapper.selectVideoName(videoName);
        if (count >= 1) {
            return false;
        }
        return true;
    }
}